package org.example.myleet.p911;

import org.example.myleet.Utils.Assert;
import org.example.myleet.Utils.JsonUtils;
import org.example.myleet.Utils.Parser;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) {
        try {
            execution("[null, 0, 1, 1, 0, 0, 1]", Parser.parse("[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]]"), Parser.parse("[[3], [12], [25], [15], [24], [8]]"));
            execution("[null,1,2,1,2,2,1,2,0,1,2]", Parser.parse("[[0,1,2,2,1],[20,28,29,54,56]]"), Parser.parse("[[28],[53],[57],[29],[29],[28],[30],[20],[56],[55]]"));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    private static void execution(String answer, int[][] initParam, int[][] params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class c = TopVotedCandidate.class;
        Constructor constructor = c.getConstructor(int[].class, int[].class);
        Object o = constructor.newInstance(initParam);
        Method m = c.getMethod("q", int.class);
        Integer[] a = new Integer[params.length + 1];
        for (int i = 0; i < params.length; ++i) {
            a[i + 1] = (Integer) m.invoke(o, params[i][0]);
        }
        Assert.isTrue(answer.equals(JsonUtils.toJson(a)));
    }
}
